﻿using System;
using System.Text;

namespace CommonUtils
{
    public class ProcessExcuteInfo
    {
        /// <summary>
        /// 进程详情
        /// </summary>
        public string StartInfo;

        public string OutputInfo = "";
        public void AppendOutputInfo(string msg)
        {
            ConsoleUtil.WriteLine("pi->" + msg);
            OutputInfo += msg + "\r\n";
        }

        public string WarningOrError = "";

        public void AppendWarningOrError(string msg)
        {
            ConsoleUtil.WriteLine("pw->" + msg);
            WarningOrError += msg + "\r\n";
        }

        public bool HasWarningOrError => !string.IsNullOrWhiteSpace(WarningOrError);

        static string[] defaultMaybeErrorKeywords = { "failed", "fatal", "error", "超时" };
        public bool MaybeError(params string[] keywords)
        {
            if (keywords.Length == 0)
                keywords = defaultMaybeErrorKeywords;
            return WarningOrError.ContainsIgnoreCase(keywords);
        }

        public string Result
        {
            get
            {
                var message = new StringBuilder();
                message.AppendLine("[Output]");
                message.AppendLine(OutputInfo);
                message.AppendLine();
                message.AppendLine("[ErrorOrWarning]");
                message.AppendLine(WarningOrError);
                return message.ToString();
            }
        }

        public override string ToString()
        {
            var message = new StringBuilder(StartInfo);
            message.AppendLine();
            message.Append(Result);
            return message.ToString();
        }

        public static ProcessExcuteInfo Default = new ProcessExcuteInfo { StartInfo = "Process not run." };
    }
}
